/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package creathtmltable;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author nadaalhirabi
 */
public class CreatHTMLtable {

    /**
     * @param args the command line arguments
     */
 public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here
        
        
        
   //---------------- read the file
     
            // reading the file names from the user
//        Scanner User_input=new Scanner(System.in);
//        String file_path, Dom_file_path;
//        
//        System.out.print("Enter the file called (Complete ...) with its path:");
//        file_path=User_input.next();
     
        Scanner User_input=new Scanner(System.in);
        String file, organism;
       
        System.out.print("Enter FeaturesLinkedFile file:");
        file=User_input.next();
        System.out.print("Enter organism name:");
        organism=User_input.next();
        
            String domtoken = "";    
     List<String> Domtemps;
     int domlength=0;
        try (Scanner inFile = new Scanner(new File(file)).useDelimiter("\n")) {
            Domtemps = new LinkedList<>();
            while (inFile.hasNext()) {
                // find next line
                domtoken = inFile.next();
                // System.out.print(token1);
                // System.out.println();             
                Domtemps.add(domtoken);
                domlength++;
            }   }

    String[] tempsDomArray = Domtemps.toArray(new String[0]); 
    String[] filearray = new String[domlength];

     int b=0;
        for (String st : tempsDomArray) {//llop for all the file// ecopy to array
        filearray[b++]=st;
        }
    ////------------////     
            
    String[] Htmltable = new String[1];
   
    
    //------------------
        Htmltable[0]="<table class=\"display\" id=\"GeneTable\" style=\"table-layout: fixed\" width=\"auto\">";//create table
        Htmltable[0]+="<tr style=\"color:#3e90ed\" > <td><form><input type=\"checkbox\" name=\"checkbox\" value=\"row\" disabled ></form></td><td class=\"col1\">Gene ID</td><td class=\"col2\">Gene length</td><td class=\"col3\">Gene model</td><td class=\"col4\">Protein length</td><td class=\"col5\">Protein Domain architecture</td></tr >";
        
        for(int i=0; i<filearray.length;++i ){
       // for(int i=0; i<20;++i ){//to test only some rows
            
            String[] parts=filearray[i].split("\t");
            String type=parts[0];//try to find gene type only
            // System.out.println(type);

            if(type.equals("gene")){//if it is gene we do loop to see the exons and draw them
                String noDomain="true";
                int start = Integer.parseInt(parts[1]);
		int end = Integer.parseInt(parts[2]);
                int genelength= end-start;
                String geneID= parts[3];
                
                 Htmltable[0]+="<tr>";//create row
                 Htmltable[0]+="<td><form><input type=\"checkbox\" name=\"checkbox\" value=\"row\"></form></td>";
                 Htmltable[0]+="<td class=\"col1\">"+ geneID +"</td> ";
                 Htmltable[0]+="<td class=\"col2\">"+ genelength +"</td> ";
                 //Htmltable[0]+="<td class=\"col3\">"+ "A_acidus_CBS_106_47_"+geneID+".svg" +"</td> ";
                 Htmltable[0]+="<td class=\"col3\">  <img src=\"GeneModelNormalizedSVG/"+geneID+"_SVG.svg\" class='svg genem-link'>     </td> ";
                 
                 
                 //Dom chicking -----------------------------------------------
                i++;
                parts=filearray[i].split("\t");
                type=parts[0];//try to find gene type only
        
                
                float protLength;    
                if(type.equals("Dom")){
                noDomain="false";
                 protLength= Integer.parseInt(parts[2]);//protein length
                
                i++;
                parts=filearray[i].split("\t");
                type=parts[0];//try to find gene type only
                 while(type.equals("Dom")){//start creating the domains
                     ++i;
                parts=filearray[i].split("\t");
                type=parts[0];//try to find gene type only
                 }
                if(noDomain.equals("false")) {
                         Htmltable[0]+="<td class=\"col4\">"+ (int)protLength +"</td> ";
                         Htmltable[0]+="<td class=\"col5\">  <img src=\"DomainArchitectureNormalizedSVG/"+geneID+"_OverlapedDom.svg\" class='svg dom-link'>     </td> ";
                        }
                //writeTofile(DomSVGfile, "A_acidus_CBS_106_47_"+geneID+"_OverlapedDom");
                 
                }//end if dom
                
                if(noDomain.equals("true")){
                    Htmltable[0]+="<td class=\"col2\"> </td> ";
                         Htmltable[0]+="<td class=\"col3\">  </td> ";
                }
                
                Htmltable[0]+="</tr>";//close row
                
                //mRNA chicking -----------------------------------------------
                 while(type.equals("mRNA")){
                    ++i;
                    parts=filearray[i].split("\t");
                    type=parts[0];
                     
                 }
                  //mRNA chicking -----------------------------------------------
                 while(type.equals("exon")){
                    ++i;
                    parts=filearray[i].split("\t");
                    type=parts[0];
                     
                 }
                 if(type.equals("CDS")){ continue; }
                            
            }//end if gene
            
           
            
        
    }//end loop on the array
         Htmltable[0]+="</table>";// close create table
            writeTofile(Htmltable, organism+"_HTMLTable");
        
  //System.out.println(Htmltable[0]);
         
    }//end main
    
    
public static void writeTofile(String[] digestedfile,  String filename) throws FileNotFoundException, IOException{
    
//System.out.println(digestedfile[0]);  

    BufferedWriter writer = null;
    try {


File file = new File(filename+".txt");  
PrintWriter out = new PrintWriter(new FileWriter(file));  
  
// Write each string in the array on a separate line  
for ( int i=0; i<digestedfile.length ;++i) {  
    String s =digestedfile[i];
    out.println(s);  
   //if(s.equals("##END"))break;
    
}  
  
out.close();  

    } catch(IOException ex) {
        ex.printStackTrace();
    }  
   
}//end write to file
    
}
